package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.taptech.doufu.util.DensityUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.shelf.BookShelfPopularize;
import com.taptech.doufu.services.history.ReadRecord;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ImageManager;

import java.util.List;

/**
 * Created by op0509 on 2016/5/27.
 */
public class BookshelfGalleryAdapter extends BaseAdapter {
    private Context context;
    private List<BookShelfPopularize> popularizeList;
    private List<ReadRecord> readRecordList;//阅读历史
    private ItemViewHolder viewHolder;

    public BookshelfGalleryAdapter(Context context, List<BookShelfPopularize> popularizeList, List<ReadRecord> readRecordList) {
        this.context = context;
        this.popularizeList = popularizeList;
        this.readRecordList = readRecordList;
    }


    public void updateReadRecordList(List<ReadRecord> list) {
        if (list != null) {
            this.readRecordList = list;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        int readRecordSize = 0;
        if (readRecordList != null) {
            readRecordSize = readRecordList.size();
        }
        if(popularizeList != null && popularizeList.size() > 0){
            return  popularizeList.size() + readRecordSize;
        }
        return readRecordSize;
    }

    @Override
    public Object getItem(int position) {
        return readRecordList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_gallery, parent, false);
            viewHolder = new ItemViewHolder();
            viewHolder.imgIcon =  convertView.findViewById(R.id.hori_imgicon);
            viewHolder.tvTitle =  convertView.findViewById(R.id.hori_name);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ItemViewHolder) convertView.getTag();
        }

        int index = position;
        if(popularizeList != null && popularizeList.size() > 0){
            index = position - popularizeList.size();
        }

        if(popularizeList != null && popularizeList.size() > 0 && position < popularizeList.size()){
            viewHolder.tvTitle.setText(popularizeList.get(position).getTitle());
            if(popularizeList.get(position).getImgUrl()!=null){
                GlideUtil.displayImage(viewHolder.imgIcon, popularizeList.get(position).getImgUrl(),R.drawable.img_default_loading_l);
            }
        }else if(readRecordList.get(index)!=null){
            viewHolder.tvTitle.setText(readRecordList.get(index).getName());
            if(readRecordList.get(index).getImg_url()!=null){
                GlideUtil.displayImage(viewHolder.imgIcon, readRecordList.get(index).getImg_url(),R.drawable.img_default_loading_l);
            }
        }

        return convertView;
    }

    class ItemViewHolder
    {
        private TextView tvTitle;
        private ImageView imgIcon;
    }

}
